﻿#include "Mystring.h"

namespace Ysz 
{
	
	const size_t string::npos = -1;
	string::string(const char* str)
		:_size(strlen(str))
	{
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, str);
	}


	string::string(const string& s)
		:_size(s._size)
	{
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, s._str);
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* str = new char[n+1];
			strcpy(str, _str);
			delete[] _str;
			_str = str;
			_capacity = n;
		}

	}

	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_capacity = _size = 0;
	}

	const char* string::c_str()const
	{
		return _str;
	}

	void string::push_back(char c)
	{
		if (_size == _capacity)
		{
			reserve(2 * _capacity > 0 ? 2 * _capacity : 4);
		}
		_str[_size] = c;
		_size++;
		_str[_size] = '\0';
	}
	void string::append(const char* str)
	{
		int strlens = strlen(str);
		if ((_size + strlens) > _capacity)
		{
			size_t newcapacit = 2 * _capacity > 0 ? 2 * _capacity : 4;
			if ((_size + strlens) > newcapacit)
			{
				newcapacit = _size + strlens;
			}
			reserve(newcapacit);
		}
		for (int i = 0; i < strlens; i++)
		{
			_str[_size + i] = str[i];
		}
		_size += strlens;
		
		_str[_size] = '\0';
	}

	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}
	string& string::operator=(const string& s)
	{
		if (this != &s)
		{
			clear();
			reserve(s._size);
			strcpy(_str, s._str);
			_size = s._size;
			return *this;
		}
		
	}
	string& string::operator+=(char c)
	{
		push_back(c);
		return *this;
	}
	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;

	}
	void string::swap(string& s)
	{
		string  tmp = s;
		s = *this;
		*this = tmp;
	}
	size_t string::size()const
	{
		return _size;
	}

	size_t string::capacity()const
	{
		return _capacity;
	}
	bool string::empty()const
	{
		if (_size == 0)
		{
			return true;
		}
		return false;
	}

	void string::resize(size_t n, char c)
	{
		if (_size > n)
		{
			_str[n] = '\0';
			_size = n;
		}
		if (_size < n)
		{
			reserve(n);
			for (size_t i = _size; i < n;i++)
			{
				_str[i] = c;
			}
			_str[n] = '\0';
			_size = n;
		}
	}

	char& string::operator[](size_t index)
	{
		assert(index < _size);

		return _str[index];
	}
	const char& string::operator[](size_t index)const
	{
		assert(index < _size);

		return _str[index];
	}
	string& string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len >= _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
			return *this;
		}
		size_t i;
		for (i = 0; i < _size - pos; i++)
		{
			_str[pos + i] = _str[pos + len + i];
			
		}

		_str[pos + i] = '\0';
		_size = _size - len;;
		return *this;
	}
	string::iterator string::begin()
	{
		return _str;
	}

	string::iterator string::end()
	{
		return &_str[_size];
	} 


	size_t string::find(char c, size_t pos) const
	{
		assert(pos < _size);
		size_t i;
		for (i = pos; i < _size; i++)
		{
			if (_str[i] == c)
			{
				return i;
			}
		}
		return npos;
	}

	size_t string::find(const char* s, size_t pos) const
	{
		assert(pos < _size);
		
		char * n = strstr(&_str[pos], s);
		if (n == nullptr)
		{
			return npos;
		}
		return n - _str;
	}

	string& string::insert(size_t pos, char c)
	{
		assert(pos <= _size);
		if (pos == _size)
		{
			push_back(c);
			return *this;
		}
		reserve(_size + 1);
		size_t i;
		for (i = _size; i > pos; i--)
		{
			_str[i] = _str[i - 1];
		}
		_str[pos] = c;
		_str[++_size] = '\0';
		return *this;
	}
	string& string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);

		if (pos == _size)
		{
			append(str);
			return *this;

		}
		size_t len = strlen(str);

		reserve(_size + len);
		_size = _size + len;
		_str[_size] = '\0';


		size_t end = _size - 1;

		for (size_t i = end; i >= pos+len; i--)
		{
			_str[i] = _str[i-len];
		}
		strncpy(&_str[pos], str, len);
		
		return *this;
		
	}
	bool string::operator==(const string& s)
	{
	
		return !(strcmp(_str, s._str));
		
	}
	bool string::operator<(const string& s)
	{
		return strcmp(_str, s._str) < 0;
	}
	bool string::operator>(const string& s)
	{
		return strcmp(_str, s._str) > 0;

	}
	bool string::operator<=(const string& s)
	{
		return *this < s || *this == s;
	}
	bool string::operator>=(const string& s)
	{
		return *this > s || *this == s;
	}

	bool string::operator!=(const string& s)
	{
		return !(*this == s);
	}
	ostream& operator<<(ostream& _cout, const string& s)
	{
		_cout << s.c_str();

		return _cout;
	}
	istream& operator>>(istream& _cin, string& s)
	{
		s.clear();
		char buff[1024] = { '\0' };
		char c = _cin.get();
		int i = 0;
		while (c != '\n' && c != ' ')
		{
			buff[i++] = c;
			if (i == 1023)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			c = _cin.get();
		}
		if (i != 0)
		{
			buff[i] = '\0';
			s += buff;
		}
		return _cin;
		
	}

	istream& getline(istream& in, string& s, char delim)
	{
		s.clear();
		char buff[1024] = { '\0' };
		char c = in.get();
		int i = 0;
		while (c != delim)
		{
			buff[i++] = c;
			if (i == 1023)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			c = in.get();
		}
		if (i != 0)
		{
			buff[i] = '\0';
			s += buff;
		}
		return in;
	}


	string string::substr(size_t pos, size_t len)
	{
		assert(pos < _size);
		size_t leftlen = _size - pos;
		if (len > leftlen)
			len = leftlen;

		string tmp;
		tmp.reserve(len);
		for (size_t i = 0; i < len; i++)
		{
			tmp += _str[pos + i];
		}

		return tmp;
	}





}

